/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of Symbol Matching.
 *
 * Symbol Matching is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * Symbol Matching is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Symbol Matching.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.reaktiopeli.game;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.JPanel;


public class ButtonsPanel extends JPanel {

	private static final long serialVersionUID = 6010484239637422667L;
	
	private ArrayList<BufferedImage> buttonImages;

	public ButtonsPanel() {
		buttonImages = new ArrayList<BufferedImage>();
	}
	
	public void clearImages()
	{
		buttonImages.clear();
	}
	
	public void addImage(BufferedImage img)
	{
		if(img!=null)
			buttonImages.add(img);
	}

	public void paintComponent(Graphics g) {
        super.paintComponent(g);  
        Graphics2D g2 = (Graphics2D) g;
        if(buttonImages==null || buttonImages.size()==0)
        {
			g2.drawRect(0, 0, this.getWidth()-1, this.getHeight()-1);
        	return;
        }
        
		int btnHeight = this.getHeight();
		int btnWidth = this.getWidth()/buttonImages.size();
		
		for(int i=0;i<buttonImages.size(); i++) {
			int imgWidth = buttonImages.get(i).getWidth();
			int imgHeight = buttonImages.get(i).getHeight();
			imgHeight = (int)(imgHeight * btnWidth * 0.9 / imgWidth);
			imgWidth = (int)(btnWidth * 0.9);
			if(imgHeight>btnHeight * 0.9)
			{
				imgWidth = (int)(imgWidth * btnHeight * 0.9 / imgHeight);
				imgHeight = (int)(btnHeight * 0.9);
			}
			g2.drawImage(buttonImages.get(i), i*btnWidth + (btnWidth-imgWidth)/2, 
					(btnHeight-imgHeight)/2, imgWidth-1, imgHeight-1, null);
			g2.drawRect(btnWidth*i, 0, btnWidth-1, btnHeight-1);
		}
	}
}
